﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FitnessDiary.Domain.Abstract;
using FitnessDiary.WebUI.ControllerHelpers;
using FitnessDiary.WebUI.Account;
using FitnessDiary.WebUI.Models;
using System.Web.Helpers;
using FitnessDiary.WebUI.Filters;

namespace FitnessDiary.WebUI.Controllers
{
	[InitializeSimpleMembership]
	[Authorize]
    public class DiaryController : Controller
    {
		private IFitnessDiaryRepository repository;
		private IUserProvider account;

		public DiaryController(IFitnessDiaryRepository repository, IUserProvider account)
		{
			this.repository = repository;
			this.account = account;
		}

		public ActionResult Index()
		{
			DateTime today = DiaryHelper.Today(tzCookieValue());
			DiaryModel model = DiaryHelper.GetDiaryModel(repository, today, 0, account.UserId);
			return View(model);
		}

		public ActionResult Next(int week)
		{
			DateTime today = DiaryHelper.Today(tzCookieValue());
			DiaryModel model = DiaryHelper.GetDiaryModel(repository, today, week, account.UserId);
			return View("Index", model);
		}

		public ActionResult Prev(int week)
		{
			DateTime today = DiaryHelper.Today(tzCookieValue());
			DiaryModel model = DiaryHelper.GetDiaryModel(repository, today, -week, account.UserId);
			return View("Index", model);
		}

		public string Time()
		{
			return DiaryHelper.GetUserLocalTime(tzCookieValue()).ToString();
		}

		public string tzCookieValue()
		{
			string cookieKey = "timezoneoffset";
			if (Request.Cookies[cookieKey] != null)
			{
				return Request.Cookies[cookieKey].Value;
			}
			else
			{
				return "0";
			}
		}
    }
}
